﻿#include "PlayersViewModel.h"

#include "CardViewModel.h"

using namespace Views::Models::Network;

PlayersViewModel::PlayersViewModel(std::queue<std::pair<Cards::PlayerCard*, Players::PLAYER_TYPE>> players)
	: players(players)
{
}

PlayersViewModel::PlayersViewModel(Cards::PlayersCards players_cards)
	: players_cards(players_cards)
{
}

sf::Packet& Views::Models::Network::operator <<(sf::Packet& Packet, const PlayersViewModel& model)
{
	Packet << sf::Uint8(model.players.size());
	std::queue<std::pair<Cards::PlayerCard*, Players::PLAYER_TYPE>> players = model.players;
	while(!players.empty())
	{
		Cards::Card* foo = players.front().first;
		CardViewModel cvm(players.front().first);
		Packet << cvm;
		Packet << sf::Uint8(players.front().second);
		players.pop();
	}
	return Packet;
}

sf::Packet& Views::Models::Network::operator >>(sf::Packet& Packet, PlayersViewModel& model)
{
	sf::Uint8 size;
	Packet >> size;
	Cards::CardsCollection cards(model.players_cards, Cards::WeaponsCards(), Cards::RoomsCards());
	CardViewModel cvm(cards);
	for(int i = 0; i < size; i++)
	{
		sf::Uint8 player_type;
		Packet >> cvm >> player_type;
		model.players.push(std::make_pair(dynamic_cast<Cards::PlayerCard*>(cvm.card),
			static_cast<Players::PLAYER_TYPE>(player_type)));
	}
	return Packet;
}

